import {
	queryCode
	
} from "@/api/common";
import {
	showToast
} from "@/utils/toast";
import {
	ref,
	computed
} from "vue";
import {
	useStore
} from "@/store";

/** store实例 */
const store = useStore();

const useVerifyCode = function(text) {
	// 按钮提示文字
	let buttonText = ref(text);
	// 是否已发送验证码
	let isSend = ref(false);
	// 倒计时
	let seconds = ref(60);

	// 发送验证码
	function sendCode(type, phone) {
		if (!phone) {
			showToast("请输入手机号");
			return;
		}
		queryCode({
			type,
			phone,
			userType: 'MERCHANT',
			agentCommendationCode: 'aaaaa',
			oemUserNo: 'BU100002'
		}).then(() => {
			showToast(`${text}成功`, "success");
			isSend.value = true;
			buttonText.value = seconds.value + "s";
			let timer = setInterval(() => {
				if (seconds.value <= 0) {
					buttonText.value = text;
					clearInterval(timer);
					timer = null;
					isSend.value = false;
					seconds.value = 60;
				} else {
					buttonText.value = seconds.value-- + "s";
				}
			}, 1000);
		}).catch((error) => {
			showToast(error.data.message);
		});
	}

	return [{
		buttonText,
		isSend
	}, {
		sendCode
	}]
}

export default useVerifyCode;